package com.project.service.impl;

import com.mysql.cj.Session;
import com.project.bean.StudentBean;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 学生业务接口实现类
 */
public class StudentServiceImpl extends BaseService implements IStudentService {
    @Override
    public void add(StudentBean student) {
        //得到会话对象
        SqlSession session = this.getSession();
        //得到mapper接口实现类对象
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        //执行添加操作
        mapper.add(student);
        //提交事务
        session.commit();
        //关闭session
        session.close();
    }

    @Override
    public void del(Integer id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(Integer id, String phone) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.update(id,phone);
        session.commit();
        session.close();
    }

    @Override
    public List<StudentBean> findAll() {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public StudentBean findById(Integer id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean studentBean = mapper.findById(id);
        session.close();
        return studentBean;
    }
}
